#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <functional>
#include <sstream>

class Cpp_Next {
public:
    using Function = std::function<void(const std::vector<std::string>&)>;

    Cpp_Next() {
        defineFunction("print", [this](const std::vector<std::string>& args) {
            for (const auto& arg : args) {
                if (variables_.count(arg)) {
                    std::cout << variables_[arg] << ' ';
                } else {
                    std::cout << arg << ' ';
                }
            }
            std::cout << '\n';
        });

        defineFunction("let", [this](const std::vector<std::string>& args) {
            if (args.size() >= 2) {
                std::ostringstream oss;
                for (size_t i = 1; i < args.size(); ++i) {
                    oss << args[i] << ' ';
                }
                variables_[args[0]] = oss.str();
            }
        });

        defineFunction("if", [this](const std::vector<std::string>& args) {
            if (args.size() >= 3) {
                std::string var = args[0];
                std::string val = args[1];
                if (variables_[var] == val) {
                    executeCommand(args, 2);
                }
            }
        });

        defineFunction("for", [this](const std::vector<std::string>& args) {
            if (args.size() >= 4) {
                std::string var = args[0];
                int start = std::stoi(args[1]);
                int end = std::stoi(args[2]);
                for (int i = start; i < end; ++i) {
                    variables_[var] = std::to_string(i);
                    executeCommand(args, 3);
                }
            }
        });
    }

    void defineFunction(const std::string& name, Function func) {
        functions_[name] = func;
    }

    void run(const std::string& code) {
        auto tokens = tokenize(code);
        executeCommand(tokens, 0);
    }

private:
    std::vector<std::string> tokenize(const std::string& code) {
        std::vector<std::string> tokens;
        std::string token;
        for (char ch : code) {
            if (isspace(ch)) {
                if (!token.empty()) {
                    tokens.push_back(token);
                    token.clear();
                }
            } else {
                token += ch;
            }
        }
        if (!token.empty()) tokens.push_back(token);
        return tokens;
    }

    void executeCommand(const std::vector<std::string>& tokens, size_t start) {
        if (tokens.empty()) return;

        std::string command = tokens[start];
        if (functions_.count(command)) {
            functions_[command](std::vector<std::string>(tokens.begin() + start + 1, tokens.end()));
        }
    }

    std::unordered_map<std::string, Function> functions_;
    std::unordered_map<std::string, std::string> variables_;
};

int main() {
    Cpp_Next script;

    // 运行 Cpp_Next 代码
    script.run("let x 5");
    script.run("let y 10");
    script.run("if x 5 print x is 5");
    script.run("for i 0 3 print i");

    return 0;
}

